#ifndef DOWNWIDGET_H
#define DOWNWIDGET_H
#include <QTcpServer>
#include <QWidget>
#include <QTcpSocket>
#include <QPushButton>
#include <QTextEdit>
#include <QLineEdit>
#include <QLabel>

#define PACK_HEAD 0x55aa
//#pragma pack(1)
struct CommonPack
{
    uint16_t head;
    uint16_t crc;
    uint16_t packId;
    uint16_t datalen;
    uint8_t dat[0];
};


struct FunctionPack
{
    uint16_t functionCode;
    uint16_t statusCode;
    uint8_t dat[0];
};

struct ResourceAckPack
{
    uint16_t functionCode;
    uint16_t statusCode;
    uint32_t sourceSize;
    uint16_t packSum;
};

class DownWidget : public QWidget
{
    Q_OBJECT
private:
    QPushButton *openFileButton;

    QTcpServer *tcpServer;
    QTcpSocket *tcpSocket;

    QPushButton *listenButton;
    QLabel *listenPortLabel;
    QLineEdit *listenLineEdit;

    QByteArray binDatArr;
    QByteArrayList datArrList;

    QTextEdit *infoTextEdit;

    bool checkDat(const char *pDat, uint16_t dataLen);
    bool SendPack(QByteArray &dat, uint16_t packId);
public slots:
    void NewConnectSlot();
    void SocketReadyReadSlot();
    void ListenButtonClickedSlot();
    void OpenFileSlot();
    void DatHandle(QByteArray &datArr);
public:
    DownWidget(QWidget *parent = nullptr);
    ~DownWidget();
};
#endif // DOWNWIDGET_H
